﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Media;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Net.NetworkInformation;


namespace VisualTail {

    /// <summary>
    /// Contenedor de ventanas.
    /// </summary>
    public partial class MDI : Form {

        //private int childFormNumber = 0;
        /// <summary>
        /// Fuente de datos.
        /// </summary>
        public DSWorkflow ds = new DSWorkflow();
        /// <summary>
        /// Ubicacion actual del ejecutable.
        /// </summary>
        public string path;

        //public string idWorkflow;

        /// <summary>
        /// Constructor.
        /// </summary>
        public MDI() {
            InitializeComponent();
            path = Application.StartupPath;
            notifyIcon1.ContextMenuStrip = contextMenuNotifyIcon;
        }

        void backgroundWorkerPing_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {

        }

        private void log(string fichero, string mensaje, int intentos) {
            try {
                using (System.IO.StreamWriter sw = System.IO.File.AppendText(fichero)) {
                    sw.Write(DateTime.Now);
                    sw.WriteLine(mensaje);
                    sw.Flush();
                    sw.Close();
                }
            } catch (Exception ex) {
                System.Console.WriteLine(ex.Message);
                System.Console.WriteLine(ex.StackTrace);
                System.Threading.Thread.Sleep(2000);
                if (intentos > 0) {
                    log(fichero, mensaje, intentos - 1);
                }
            }
        }


        void sp_LoadCompleted(object sender, AsyncCompletedEventArgs e){
            SoundPlayer sp = (SoundPlayer)sender;
            sp.Play();
        }




        private void OpenFile(object sender, EventArgs e) {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "Archivos de texto (*.txt)|*.txt|Todos los archivos (*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
                string FileName = openFileDialog.FileName;
                // TODO: agregar código aquí para abrir el archivo.
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e) {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Archivos de texto (*.txt)|*.txt|Todos los archivos (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
                // TODO: agregar código aquí para guardar el contenido actual del formulario en un archivo.
            }
        }


        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e) {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticleToolStripMenuItem_Click(object sender, EventArgs e) {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e) {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e) {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e) {
            foreach (Form childForm in MdiChildren) {
                childForm.Close();
            }
        }

        private void MDI_Load(object sender, EventArgs e) {
            string filename = path + "\\DSWorkflow.xml";
            //System.IO.Stream fichero = null;
            if (System.IO.File.Exists(filename) == false) {
                DSWorkflow tmp = new DSWorkflow();
                tmp.WriteXml(filename);
            }
            this.ds.ReadXml(filename);
            //http://www.josecuellar.net/microsoft-asp-net/generar-documentacion-chm-o-web-mediante-visual-studio-y-sandcastle
            helpProvider1.HelpNamespace = path + "\\Ayuda\\VisualTail.chm";

            notifyIcon1.ShowBalloonTip(2, "Visual Tail", "Puede minimizar VisualTail y acceder desde esta posición.", ToolTipIcon.Info);
        }

        /// <summary>
        /// Hace invisible el contenedor cuando se minimiza.
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m) {
            base.WndProc(ref m);
            if (((int)m.WParam & 0xFFF0) == 0xF020){
                //MINIMIZANDO
                this.Visible = false;
            }

        }

        private void guardarDataSet() {
            string filename = path + "\\DSWorkflow.xml";
            ds.WriteXml(filename);
        }

        /// <summary>
        /// Guarda los datos de la fuente de datos.
        /// </summary>
        public void SaveData(){
            try {
                guardarDataSet();
                MessageBox.Show("Information saved!", this.Text);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Error saving data.");
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
            AboutBox frm = new AboutBox();
            frm.MdiParent = this;
            frm.Show();
        }


        private void RestoreMe() {
            this.Visible = true;
            this.WindowState = FormWindowState.Maximized;
            this.BringToFront();
            this.Focus();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e) {
            this.Visible = false;
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
            RestoreMe();
        }

        private void restaurarDWHWorkflowToolStripMenuItem_Click(object sender, EventArgs e) {
            RestoreMe();
        }

        private void indexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this, helpProvider1.HelpNamespace);
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e) {
            SaveData();
            Application.Exit();
        }

        private void tailToolStripMenuItem_Click(object sender, EventArgs e) {
            Tail frm = new Tail();
            frm.MdiParent = this;
            frm.SetDSWorkflow(this.ds);
            frm.Show();
        }

        private void optionsToolStripMenuItem1_Click(object sender, EventArgs e) {
            Opciones frm = new Opciones();
            frm.MdiParent = this;
            frm.Show();
        }

        private void saveDataToolStripMenuItem_Click(object sender, EventArgs e) {
            SaveData();
        }
    }
}
